package modelLayer;

import java.util.ArrayList;

import uiLayer.OfficeUI;

import controllerLayer.SaleCtr;

public class Sale {
	private int id;
	private ArrayList<SaleLinesItem> shoppingCart;
	
	private Customer customer;
	private Employee employee;
	private SaleCtr sctr;
	public Sale(int id,Customer customer,
			Employee employee,ArrayList<SaleLinesItem> shoppingCart) {
		this.id = id;
		this.shoppingCart=shoppingCart;
		this.customer = customer;
		this.employee = employee;
		sctr = new SaleCtr();
	}
	public ArrayList<SaleLinesItem> getShoppingCart() {
		return shoppingCart;
	}
	public void setShoppingCart(ArrayList<SaleLinesItem> shoppingCart) {
		this.shoppingCart = shoppingCart;
	}
	public Customer getCustomer() {
		return customer;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void addItem(SaleLinesItem pr1)
	{
		shoppingCart.add(pr1);
	}
	
	public String printOffer(int id){
		Sale sale = sctr.findSale(id);
		double totalPrice = 0;
		String offer = null;
		try{
			offer = sale.getEmployee().toString();
			offer = offer + "/n"+sale.getCustomer().toString();
		}
		catch(NullPointerException npe){
			System.out.println("Something went wrong");
			OfficeUI.getInstance().run();
		}

		for(SaleLinesItem sl:shoppingCart){
			totalPrice = totalPrice + sl.getPrice();
			offer = offer +(sl.getName() + ": "+ sl.getBuyQuantity()+sl.getMeasure());
		}
		offer = offer +(totalPrice);
		return offer;
	}
	public String printDispatchNote(int id){
		Sale sale = sctr.findSale(id);
		String offer = null;
		offer = sale.getEmployee().toString();
		offer = offer + "/n"+sale.getCustomer().toString();
		for(SaleLinesItem sl:shoppingCart){
			offer = offer +(sl.getName() + ": "+ sl.getBuyQuantity()+sl.getMeasure()+sl.getProduct().getLocation());
		}
		return offer;
	}
	public String printDeliveryNote(int id){
		Sale sale = sctr.findSale(id);
		String offer = null;
		offer = sale.getEmployee().toString();
		offer = offer + "/n"+sale.getCustomer().toString();
		for(SaleLinesItem sl:shoppingCart){
			offer = offer +(sl.getName() + ": "+ sl.getBuyQuantity()+sl.getMeasure());
		}
		return offer;
	}
	@Override
	public String toString() {
		return "Sale [shoppingCart=" + shoppingCart + ", customer=" + customer
				+ ", employee=" + employee + "]";
	}
	public String save(){
		String prods = "";
		for(SaleLinesItem s1: shoppingCart){
			try{
				prods = prods + s1.getID();
			}
			catch(NullPointerException npe){
				
			}
		}
			return 4+","+id+","+customer.getId()+","+employee.getId()+","+prods;
	}
	public int getId() {
		
		return id;
	}
}
